<?php

namespace addons\shopro\controller;

use app\admin\model\shopro\activity\Activity as ActivityModel;
use app\admin\model\shopro\activity\ActivityOrder as ActivityOrderModel;
use addons\shopro\model\commission\Agent as AgentModel;
class Activity extends Base
{

    protected $noNeedLogin = ["*"];
    protected $noNeedRight = ['*'];


    /**
     * 活动列表
     */
    public function lst()
    {
        $params = $this->request->get();

        $list = ActivityModel::where('is_show', 1)->order('id', 'desc')->paginate($per_page ?? 10)->toArray();
        $agentModel = new AgentModel();
        $childs = $agentModel->getChild($this->auth->id);
        for($i = 0;$i < count($list['data']);$i++){
            $order = ActivityOrderModel::getInfo($this->auth->id,$list['data'][$i],$childs);
            $list['data'][$i] = array_merge($list['data'][$i],$order);
        }
        $this->success('获取成功', $list);
    }
    public function detail()
    {
        $id = $this->request->get('id');
        $data = ActivityModel::with("richtext")->find(['id' => $id]);
        $data = json_decode(json_encode($data),true);
        $order = ActivityOrderModel::getInfo($this->auth->id,$data);
        $data = array_merge($data,$order);
        $this->success($data['title'], $data);
    }
    public function apply(){
        $activity_id = $this->request->post('activity_id');
        $data = ActivityModel::where("id",$activity_id)->find();
        if(!$data) {
            $this->error('活动不存在或已下架');
        }else if($data['sign_status'] == 'nostart'){
             $this->error('活动报名未开始');
        } else if($data['sign_status'] == 'ended'){
             $this->error('活动报名已结束');
        } else if($data['status'] == 'ended'){
             $this->error('活动已结束');
        } else {
            $agent = AgentModel::where("user_id",$this->auth->id)->where("status","normal")->find();
            if(!$agent){
                $this->error('您还不是代理，不能报名活动');
            }
            if(!$data['agent_level']){
                $activity_level = explode(",",$data['agent_level']);
                if(!in_array($agent['level'],$activity_level)){
                    $this->error('您的代理等级不允许报名此活动');
                }
            }
            $activity_order = ActivityOrderModel::where("user_id",$this->auth->id)->where("activity_id",$activity_id)->find();
            if(!$activity_order){
                $activity_order = ActivityOrderModel::create([
                    'user_id' => $this->auth->id,
                    'activity_id' => $activity_id,
                    'pay_status' => 0,
                    'order_sn' => self::getSn($this->auth->id),
                    'total_fee' => $data['fee']
                ]);
            } else if($activity_order['pay_status'] == 0){
                $activity_order->order_sn = self::getSn($this->auth->id);
                $activity_order->total_fee = $data['fee'];
                $activity_order->save();
            }
        }
        $this->success('提交成功',$activity_order);
    }
    public function applyOrder(){
        $id = $this->request->post('id');
        $activity_order = ActivityOrderModel::where("user_id",$this->auth->id)->where("id",$id)->find();
        if(!$activity_order){
            $this->error('订单不存在');
        } 
        $this->success('提交成功',$activity_order);
    }
     // 获取订单号
    public static function getSn($user_id)
    {
        $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
        $order_sn = date('Yhis') . $rand;

        $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);

        return 'AC' . $order_sn . $id;
    }
    // 领取奖励
    public function receiveReward(){
        $activity_id = $this->request->post('activity_id');
        $data = ActivityModel::where("id",$activity_id)->find();
        $data = json_decode(json_encode($data),true);
        $order = ActivityOrderModel::getInfo($this->auth->id,$data);
        $data = array_merge($data,$order);
        if($data['receive_status'] == 0){
            ActivityOrderModel::where("user_id",$this->auth->id)->where("activity_id",$activity_id)->update(['status'=>1]);
            $this->success('提交成功',['receive_status'=>1,'receive_status_text'=>"审核中"]);
        }
        $this->error($data['receive_status_text']);
    }
}
